package com.party.admin.web.controller.menu;

import com.party.admin.biz.system.MenuBizService;
import com.party.admin.web.dto.AjaxResult;
import com.party.common.paging.Page;
import com.party.core.model.menu.Menu;
import com.party.core.service.menu.IMenuService;
import com.party.core.service.system.ISysPrivilegeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


@Controller
@RequestMapping(value = "/system/menu")
public class MenuController {
    @Autowired
    private IMenuService menuService;

    @Autowired
    private ISysPrivilegeService privilegeService;

    @Autowired
    private MenuBizService menuBizService;

    @ResponseBody
    @RequestMapping(value = "/list")
    public ModelAndView list(Menu menu, Page page) {
        ModelAndView mv = new ModelAndView("system/menu/list");
        mv.addObject("tree", menuBizService.getListTree());
        return mv;
    }

    @ResponseBody
    @RequestMapping(value = "/data")
    public AjaxResult list() {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setData(menuBizService.getListTree());
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }
    /**
     * 排序
     */
    @ResponseBody
    @RequestMapping(value = "sort")
    public AjaxResult sort(String[] sortList) {
        AjaxResult ajaxResult = new AjaxResult();
        for(Integer i=0; i<sortList.length;i++){
            Menu menu = menuService.get(sortList[i]);
            String sort = Integer.toString(i);
            if(!menu.getSort().equals(sort)){
                menu.setSort(sort);
                menuService.update(menu);
            }
        }
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }
    /**
     * 删除
     */
    @ResponseBody
    @RequestMapping(value = "del")
    public AjaxResult del(String id) {
        AjaxResult ajaxResult = new AjaxResult();
        boolean delete = menuService.delete(id);
        ajaxResult.setSuccess(delete);
        return ajaxResult;
    }
    /**
     * 保存菜单
     *
     * @param menu 菜单
     * @return 交互数据
     */
    @ResponseBody
    @RequestMapping(value = "save")
    public AjaxResult save(Menu menu, String permission) {
        AjaxResult ajaxResult = new AjaxResult();
        Menu dbMenu = menuBizService.saveBiz(menu, permission);
        ajaxResult.setData(dbMenu);
        ajaxResult.setSuccess(true);
        return ajaxResult;
    }
}
